package javango.forms;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javango.forms.fields.BoundField;
import javango.forms.fields.LongField;
import javango.forms.widgets.HiddenInputWidget;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.inject.Injector;

/**
 * Class representing an abstration to allow multiple forms to be displayed on the same page,  depreciated in favor of FormSets
 * @author john
 *
 */
@Deprecated 
public class InlineForms implements Iterable<Form> {

	private final Log log = LogFactory.getLog(InlineForms.class);
	
	protected final List<Form> forms = new ArrayList<Form>();
	protected final String prefix;
	protected final String countFieldName;
	protected final Form controlForm;
	protected final LongField countField;
	
	public String getPrefix() {
		return prefix;
	}

	Injector injector;
	
	public InlineForms(Injector injector, String prefix) {
		this.injector = injector;
		this.prefix = prefix;
		this.countFieldName = "fieldCount";
		
		controlForm = injector.getInstance(AbstractForm.class);
		controlForm.setPrefix(String.format("%s_CTRL", prefix));
		countField = injector.getInstance(LongField.class);
		countField.setName(countFieldName).setWidget(HiddenInputWidget.class).setRequired(false).setAllowNull(false);
		controlForm.getFields().put(countFieldName, countField);
		countField.setInitial(0L);
	}

	public String getControlFieldsHtml() {
		StringBuilder builder = new StringBuilder();
		for(BoundField bf: controlForm) {
			builder.append(bf.toString());
		}
		return builder.toString();
	}
	
	public Iterator<BoundField> getControlFields() {
		return controlForm.iterator();
	}
	
	public InlineForms(Injector injector, String prefix, String coreField, Class<? extends Form> formClass, Map<String, String[]> values) {
		this(injector, prefix);
		
		controlForm.bind(values);
		if (!controlForm.isValid()) {
			// TODO maybe take a better error message from the form
			throw new RuntimeException(String.format("Unable to find field '%s' in values.", controlForm.getFields().get(countFieldName).getName()));
		}
		
		Long fieldCount = (Long)controlForm.getCleanedData().get(countFieldName);
		if (fieldCount == null) fieldCount = 0L;
		
		for(int ctr=0; ctr<fieldCount; ctr++) {
			if (values.containsKey(prefix + "[" + ctr + "]-" + coreField)) {
				String[] coreValues = values.get(prefix + "[" + ctr + "]-" + coreField);
				if (coreValues != null && coreValues.length > 0 && StringUtils.isNotBlank(coreValues[0])) {
					// create a new form for this ctr,  passing the data map and the correct prefix.
					String formPrefix = prefix + "[" + ctr + "]";
				
					Form form = injector.getInstance(formClass)
						.setPrefix(formPrefix)
						.bind(values);
					
					forms.add(form);
				}
			}
		}
	}
	
	public boolean isValid() {
		boolean isvalid = true;
		for (Form form : forms) {
			if (!form.isValid()) isvalid = false;
		}
		return isvalid;
	}
	
	public InlineForms add(Form form) {
		Long ctr = (Long)countField.getInitial();
		if (ctr == null) ctr = 0L;
		
		form.setPrefix(prefix + "[" + ctr++ + "]");
		forms.add(form);
		
		countField.setInitial(ctr);
		
		return this;
	}

	public Iterator<Form> iterator() {
		return forms.iterator();
	}
	
	public List<Form> getFormsList() {
		return forms;
	}
	
	public int size() {
		return forms.size();
	}
	
	
}
